/**
 * 
 */
package cn.edu.tju.ldj.analytics.utils;

import java.util.List;
import java.util.Map;

import cn.com.enorth.util.cjy.StrUtil;
import cn.edu.tju.ldj.analytics.Const;
import cn.edu.tju.ldj.analytics.Factory;
import cn.edu.tju.ldj.analytics.vo.GoodType;
import cn.edu.tju.ldj.analytics.vo.components.BtnAlpha;
import cn.edu.tju.ldj.analytics.vo.components.LeftNavMenuGroup;
import cn.edu.tju.ldj.analytics.vo.components.TopNavMenu;

/**
 * @author cjy
 * 每个人必须了解，系统参数的类，参数的设置一律以key value的方式设置在reports.xml的params段中
 */
public class IUtilParam {
	private Map<String, String> maps;
	private Map<String, String> dictionary;
	private List<Integer> lineColors;
	private List<TopNavMenu> topNavMenu;
	private List<LeftNavMenuGroup> leftNavMenuGroupLst;
	private List<GoodType> goodTypeLst;
	private String webappsPath;
	private List<BtnAlpha> alphaLst;
	@SuppressWarnings("unchecked")
	public IUtilParam() throws Exception {
		maps = (Map<String, String>) Factory.utilXml.loadObject(
				Const.PARAM_FILE_GLOBAL_NAME, Const.PARAM_FILE_SECTION_PARAMS);
		dictionary = (Map<String, String>) Factory.utilXml.loadObject(
				Const.PARAM_FILE_GLOBAL_NAME, Const.PARAM_FILE_SECTION_DICT);
		lineColors = (List<Integer>) Factory.utilXml.loadObject(
				Const.PARAM_FILE_GLOBAL_NAME,
				Const.PARAM_FILE_SECTION_LINECOLORS);
		topNavMenu = (List<TopNavMenu>) Factory.utilXml.loadObject(
				Const.PARAM_FILE_GLOBAL_NAME,
				Const.PARAM_FILE_SECTION_TOPNAVMENUS);
		leftNavMenuGroupLst = (List<LeftNavMenuGroup>) Factory.utilXml.loadObject(
				Const.PARAM_FILE_GLOBAL_NAME,
				Const.PARAM_FILE_SECTION_LFTNAVMENUGROUP);
		goodTypeLst = (List<GoodType>) Factory.utilXml.loadObject(
				Const.PARAM_FILE_GLOBAL_NAME,
				Const.PARAM_FILE_SECTION_GTYPE_SET);
		webappsPath = this.getClass().getClassLoader().getResource("/")
				.getPath();
		webappsPath = webappsPath.substring(0,
				webappsPath.indexOf("webapps") + 7).replaceAll("%20", " ");
		alphaLst = (List<BtnAlpha>) Factory.utilXml.loadObject(Const.PARAM_FILE_GLOBAL_NAME, Const.PARAM_FILE_SECTION_ALPHA_SET);
	}

	/**
	 * 取得一个字符串参数
	 * @param paramName
	 * @return
	 */
	public String getParam(String paramName,String ...args) {
		String value= maps.get(paramName);
		if(null!=value){
			for(int i=0;i<args.length;i++){
				value=StrUtil.replaceAllStr(value,"#{"+i+"}", args[i]);
			}
			
		}
		return value;
	}

	/**
	 * 取得一个长整参数
	 * @param paramName
	 * @return
	 */
	public long getLongParam(String paramName) {
		return Long.parseLong(getParam(paramName));
	}

	/**
	 * 取得一个整数参数
	 * @param paramName
	 * @return
	 */
	public int getIntParam(String paramName) {
		return (int) getLongParam(paramName);
	}

	/**
	 * 取得一个bool参数
	 * @param paramName
	 * @return
	 */
	public boolean getBoolParam(String paramName) {
		return "true".equalsIgnoreCase(getParam(paramName));
	}

	public Integer getLineColors(int index) {
		return lineColors.get(index%lineColors.size());
	}

	public List<TopNavMenu> getTopNavMenu() {
		return topNavMenu;
	}

	public LeftNavMenuGroup getLeftNavMenuGroupById(String id) {
		for (int i = 0; i < leftNavMenuGroupLst.size(); i++) {
			if(leftNavMenuGroupLst.get(i).getGroupId().equals(id)){
				return leftNavMenuGroupLst.get(i);
			}
		}
		return null;
	}

	public List<GoodType> getGoodTypeLst() {
		return goodTypeLst;
	}

	public String getWebappsPath() {
		return webappsPath;
	}

	public String getDictionary(String paramName,String ...args) {
		String value= dictionary.get(paramName);
		if(null!=value){
			for(int i=0;i<args.length;i++){
				value=StrUtil.replaceAllStr(value,"#{"+i+"}", args[i]);
			}
			
		}
		return value;
	}

	public List<BtnAlpha> getAlphaLst(String alphaId) {
		if(alphaId != null){
			for (int i = 0; i < alphaLst.size(); i++) {
				if(alphaId.equals(alphaLst.get(i).getAlphaId())){
					alphaLst.get(i).setAlphaClass(Factory.utilParam.getParam(Const.PARAM_FILE_SECTION_ALPHA_CLASS_DFT));
				}else{
					alphaLst.get(i).setAlphaClass(Factory.utilParam.getParam(Const.PARAM_FILE_SECTION_ALPHA_CLASS_OPT));
				}
			}
		}
		return alphaLst;
	}
	
	public BtnAlpha getCurAlpha() {
		for (int i = 0; i < alphaLst.size(); i++) {
			if(Factory.utilParam.getParam(Const.PARAM_FILE_SECTION_ALPHA_CLASS_DFT).equals(alphaLst.get(i).getAlphaClass())){
				return alphaLst.get(i);
			}
		}
		return null;
	}

}
